#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_symbrk.info <<'%EOF%'
   geoopt_symbrk
   -------------
   Molecule:         H2O2
   Wave Function:    SCF / cc-pVDZ
   Test Purpose:     Geometry optimization (2nd order) with symmetry
                     breaking
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_symbrk.mol <<'%EOF%'
BASIS
cc-pVDZ
Geometry optimization (2nd order) with automatic breaking of symmetry

Atomtypes=2 Angstrom Generators=2   X YZ
Atoms=1 Charge=8.0
O     0.0  0.73 0.0
Charge=1.0 Atoms=1
H     0.0 0.73 0.95
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_symbrk.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
*OPTIMIZE
.2NDORDER
**WAVE FUNCTIONS
.HF
**PROPERTIES
.VIBANA
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_symbrk.check
cat >>geoopt_symbrk.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a -l"
else
   GREP="egrep -l"
fi

# Optimization setup
CRIT1=`$GREP "Default 2nd order method will be used\: * Newton method" $log | wc -l`
CRIT2=`$GREP "Optimization will be performed in Cartesian coordinates" $log | wc -l`
CRIT3=`$GREP "Trust region method will be used to control step \(default\)" $log | wc -l`
CRIT4=`$GREP "2nd Order Geometry Optimization" $log | wc -l`
TEST[1]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[1]=4
ERROR[1]="GEOMETRY OPTIMIZATION NOT SET UP CORRECTLY"

# Basis set
CRIT1=`$GREP "O * 2 * 8\.0000 * 26 * 14 * \[9s4p1d\|3s2p1d\]" $log | wc -l`
CRIT2=`$GREP "H * 2 * 1\.0000 * 7 * 5 * \[4s1p\|2s1p\]" $log | wc -l`
CRIT3=`$GREP "total\: * 4 * 18\.0000 * 66 * 38" $log | wc -l`
CRIT4=`$GREP "Spherical harmonic basis used\." $log | wc -l`
TEST[2]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[2]=4
ERROR[2]="BASIS SET NOT READ CORRECTLY"

# Geometry
CRIT1=`$GREP "Coordinates are entered in Angstrom and converted to atomic units\." $log | wc -l`
CRIT2=`$GREP -l "1 * x * (\-0|\-|0| )\.0000000000" $log | wc -l`
CRIT3=`$GREP "2 * y * 1\.37950007" $log | wc -l`
CRIT4=`$GREP "3 * z * (\-0|\-|0| )\.0000000000" $log | wc -l`
CRIT5=`$GREP -l "4 * x * (\-0|\-|0| )\.0000000000" $log | wc -l`
CRIT6=`$GREP "5 * y * \-1\.37950007" $log | wc -l`
CRIT7=`$GREP "6 * z * (\-0|\-|0| )\.0000000000" $log | wc -l`
CRIT8=`$GREP "7 * x * (\-0|\-|0| )\.0000000000" $log | wc -l`
CRIT9=`$GREP "8 * y * 1\.37950007" $log | wc -l`
CRIT10=`$GREP "9 * z * 1\.7952398" $log | wc -l`
CRIT11=`$GREP "10 * x * (\-0|\-|0| )\.0000000000" $log | wc -l`
CRIT12=`$GREP "11 * y * \-1\.37950007" $log | wc -l`
CRIT13=`$GREP "12 * z * \-1\.7952398" $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12 \+ \
		$CRIT13`
CTRL[3]=13
ERROR[3]="GEOMETRY NOT READ CORRECTLY"

# Symmetry
CRIT1=`$GREP "Number of coordinates in each symmetry\: * 4 * 2 * 4 * 2" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry\: * 14 * 5 * 14 * 5" $log | wc -l`
TEST[4]=`expr	$CRIT1 \+ $CRIT2`
CTRL[4]=2
ERROR[4]="SYMMETRY NOT CORRECT"

# Initial energy
CRIT1=`$GREP "Final * HF energy\: * \-150\.775493489" $log | wc -l`
TEST[5]=`expr	$CRIT1`
CTRL[5]=1
ERROR[5]="INITIAL ENERGY NOT CORRECT"

# Initial gradient
CRIT1=`$GREP "O * y *   0*\.07240" $log | wc -l`
CRIT2=`$GREP "H * y * \-0*\.05451" $log | wc -l`
CRIT3=`$GREP "H * z *   0*\.00310" $log | wc -l`
TEST[6]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[6]=3
ERROR[6]="INITIAL GRADIENT NOT CORRECT"

# Initial Hessian
CRIT1=`$GREP "O * y * 1\.1338" $log | wc -l`
CRIT2=`$GREP "O * z * (0| )\.1563.[0-9] * 1\.5345" $log | wc -l`
CRIT3=`$GREP "H * y * (0| )\.0368.[0-9] * (0| )\.2148.[0-9] * (0| )\.1950" $log | wc -l`
CRIT4=`$GREP "H * z * (\-0|\-)\.0546.[0-9] * \-1\.2024.[0-9] * (0| )\.0367.[0-9] * 1\.2107" $log | wc -l`
CRIT5=`$GREP "O * x * (0| )\.0005" $log | wc -l`
CRIT6=`$GREP "H * x * (\-0|\-)\.0005.[0-9] * (0| )\.0005" $log | wc -l`
CRIT7=`$GREP "O * y * (0| )\.1422" $log | wc -l`
CRIT8=`$GREP "O * z * (0| )\.0346.[0-9] * 1\.2088" $log | wc -l`
CRIT9=`$GREP "H * y * (\-0|\-)\.1422.[0-9] * (\-0|\-)\.0346.[0-9] * (0| )\.1422" $log | wc -l`
CRIT10=`$GREP "H * z * (\-0|\-)\.0346.[0-9] * \-1\.2088.[0-9] * (0| )\.0346.[0-9] * 1\.2088.[0-9]" $log | wc -l`
CRIT11=`$GREP "O * x * (0| )\.0937" $log | wc -l`
CRIT12=`$GREP "H * x * (\-0|\-)\.0412.[0-9] * (0| )\.0017" $log | wc -l`
TEST[7]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12`
CTRL[7]=12
ERROR[7]="INITIAL HESSIAN NOT CORRECT"

# Initial step
CRIT1=`$GREP "O *_1 * (0| )\.0000000000 * 1\.29998....[0-9] * (0| )\.11012....[0-9]" $log | wc -l`
CRIT2=`$GREP "O *_2 * (0| )\.0000000000 * \-1\.29998....[0-9] * (\-0|\-)\.11012....[0-9]" $log | wc -l`
CRIT3=`$GREP "H *_1 * (0| )\.0000000000 * 1\.53924....[0-9] * 1\.89300....[0-9]" $log | wc -l`
CRIT4=`$GREP "H *_2 * (0| )\.0000000000 * \-1\.53924....[0-9] * \-1\.89300....[0-9]" $log | wc -l`
TEST[8]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[8]=4
ERROR[8]="INITIAL STEP NOT CORRECT"

# Second iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-150\.78490" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.02745" $log | wc -l`
CRIT3=`$GREP "Norm of step * \: * (0| )\.0454(8|9)" $log | wc -l`
TEST[9]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[9]=3
ERROR[9]="SECOND ITERATION NOT CORRECT"

# Third iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-150\.785410" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.00143[0-9]" $log | wc -l`
CRIT3=`$GREP "Norm of step * \: * (0| )\.001(79|80)[0-9]" $log | wc -l`
TEST[10]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[10]=3
ERROR[10]="THIRD ITERATION NOT CORRECT"

# Breaking of symmetry
CRIT1=`$GREP "Due to non\-zero index of total Hessian\, symmetry \# * 1" $log | wc -l`
CRIT2=`$GREP "has to be broken to minimize energy\!" $log | wc -l`
TEST[11]=`expr	$CRIT1 \+ $CRIT2`
CTRL[11]=2
ERROR[11]="SYMMETRY NOT BROKEN CORRECTLY"

# Symmetry determination
CRIT1=`$GREP "Symmetry .* found\: C\(2\)" $log | wc -l`
CRIT2=`$GREP "The following symmetry element was found\: * YZ" $log | wc -l`
CRIT3=`$GREP "Full point group is\: * C\(2\)" $log | wc -l`
CRIT4=`$GREP "Represented as\: * C2 " $log | wc -l`
TEST[12]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[12]=4
ERROR[12]="SYMMETRY NOT DETERMINED CORRECTLY"

# New geometry
CRIT1=`$GREP "Total number of coordinates\: * 12" $log | wc -l`
CRIT2=`$GREP "2 * y * 1\.32318." $log | wc -l`
CRIT3=`$GREP "1 * x *\-*0*\.0000000000" $log | wc -l`
CRIT4=`$GREP "3 * z * \-0*\.029637135" $log | wc -l`
CRIT5=`$GREP "5 * y * \-1\.32318." $log | wc -l`
CRIT6=`$GREP -l "4 * x * \-*0*\.0000000000" $log | wc -l`
CRIT7=`$GREP "6 * z * \-0*\.029637135" $log | wc -l`
CRIT8=`$GREP "8 * y * \-*1\.65806" $log | wc -l`
CRIT9=`$GREP "7 * x * \-*1\.75853" $log | wc -l`
CRIT10=`$GREP "9 * z * 0*\.470362865" $log | wc -l`
CRIT11=`$GREP "11 * y * \-*1\.65806" $log | wc -l`
CRIT12=`$GREP "10 * x * \-*1\.75853" $log | wc -l`
CRIT13=`$GREP "12 * z * 0*\.470362865" $log | wc -l`
TEST[13]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12 \+ \
		$CRIT13`
CTRL[13]=14
# Aug. 2018: disabling this test, depends on orientation of molecule
TEST[13]=0
CTRL[13]=0
ERROR[13]="SYMMETRY-BROKEN GEOMETRY NOT CORRECT"

# Final energy
#CRIT1=`$GREP "Final * HF energy\: * \-150\.7867668(4|5)....." $log | wc -l`
# TODO: check why geoopt_symbrk not always converges to the same energy and geometry  Nov. 2013/hjaaj
CRIT1=`$GREP "Final * HF energy\: * \-150\.783313(5|6)" $log | wc -l`
TEST[14]=`expr	$CRIT1`
CTRL[14]=1
ERROR[14]="INTERMEDIATE HF ENERGY NOT CORRECT"

# Final geometry
#CRIT1=`$GREP "O *_1 *    1\.3120...... *    0*\.0988...... * \-0*\.249(5|6)...." $log | wc -l`
#CRIT2=`$GREP "O *_2 * \-*1\.3120...... * \-*0*\.0988...... * \-0*\.249(5|6)......" $log | wc -l`
#CRIT3=`$GREP "H *_1 *    1\.583[23]...... *     1\.6000...... *   0*\.6903......" $log | wc -l`
#CRIT4=`$GREP "H *_2 * \-*1\.583[23]...... *  \-*1\.6000...... *   0*\.6903......" $log | wc -l`
#TEST[15]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
TEST[15]=16
CTRL[15]=16
ERROR[15]="FINAL GEOMETRY NOT CORRECT"

# Final iteration
CRIT1=`$GREP -l "Energy at this geometry is * \: * \-150\.78676" $log | wc -l`
CRIT2=`$GREP -l "Norm of gradient           * \: * 0*\.0000" $log | wc -l`
CRIT3=`$GREP -l "Norm of step               * \: * 0*\.0000" $log | wc -l`
TEST[16]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[16]=3
ERROR[16]="FINAL ITERATION NOT CORRECT"

# Vibrational frequencies
CRIT1=`$GREP "1 * A * 4117\.4. * (0| )\.01876[0-9] * 31\.6[89][0-9] * (0| )\.7[45][09][0-9]" $log | wc -l`
CRIT2=`$GREP "3 * A * 1612\.0. * (0| )\.00734[0-9] * (0| )\.34[0-9] * (0| )\.008[0-9]" $log | wc -l`
CRIT3=`$GREP "5 * A * 1132\.5. * (0| )\.00516[0-9] * 1\.71[0-9] * (0| )\.040[0-9]" $log | wc -l`
CRIT4=`$GREP "6 * A * 377\.8. * (0| )\.00172[0-9] * 213\.2.. * 5\.046[0-9]" $log | wc -l`
CRIT5=`$GREP "2 * B * 4114\.(7|8). * (0| )\.01874[0-9] * 109\.[56].. * 2\.59[34][0-9]" $log | wc -l`
CRIT6=`$GREP "4 * B * 1469\.9. * (0| )\.00669[0-9] * 101\.19[0-9] * 2\.394[0-9]" $log | wc -l`
TEST[17]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6`
CTRL[17]=6
ERROR[17]="VIBRATIONAL FREQUENCIES NOT CORRECT"

# Geometry optimization
CRIT1=`$GREP "Geometry converged in * (9|10) * iterations\!" $log | wc -l`
CRIT2=`$GREP "Please note that symmetry was broken during this optimization" $log | wc -l`
CRIT3=`$GREP "Energy at final geometry is * \: * \-150\.786767 a\.u\." $log | wc -l`
CRIT4=`$GREP "Energy change during optimization \: * (\-0|\-)\.011273 a\.u\." $log | wc -l`
TEST[18]=`expr	$CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[18]=3
ERROR[18]="GEOMETRY NOT CONVERGED CORRECTLY"

PASSED=1
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} )"
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
